﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class GroupManage : Form
    {
        public GroupManage()
        {
            InitializeComponent();
        }

        private void GroupManage_Load(object sender, EventArgs e)
        {
            InitListView();
        }

        public void InitListView()
        {
            this.lvGroup.Items.Clear();
            DataTable dt = QT.SEO.Group.GetAll();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = (this.lvGroup.Items.Count + 1).ToString();
                item.SubItems.Add(dr["gname"].ToString());
                item.Tag = dr["guid"].ToString();
                this.lvGroup.Items.Add(item);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddGroup myGroup = new AddGroup();
            myGroup.Owner = this;
            myGroup.ShowDialog();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.lvGroup.SelectedItems.Count != 1)
            {
                MessageBox.Show("您没有选择或选择项大于一条记录");
                return;
            }

            AddGroup myGroup = new AddGroup(this.lvGroup.SelectedItems[0].SubItems[1].Text);
            myGroup.Tag = this.lvGroup.SelectedItems[0].Tag.ToString();
            myGroup.Owner = this;
            myGroup.ShowDialog();
            InitListView();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.lvGroup.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择删除项");
                return;
            }

            List<string> guids = new List<string>();
            foreach (ListViewItem item in this.lvGroup.SelectedItems) guids.Add(item.Tag.ToString());

            int rval = QT.SEO.Group.Delete(guids);

            if (rval > 0)
            {
                MessageBox.Show(string.Format("删除成功，共删除{0}条记录", rval));
                InitListView();
            }
            else MessageBox.Show("操作失败");
        }
    }
}
